package com.lcz.manage.util;

import com.lcz.manage.sys.constants.CcConstants;
import com.lcz.manage.sys.redis.SysConfigRedis;
import com.lcz.manage.sys.service.SysConfigService;
import com.lcz.manage.util.exception.CCException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.util.Date;

/**
 * 说明: 公共方法
 *
 * @author lcz <lucz25053@hsjry.com>
 * @version CFM-V1.0
 * @date 2018年10月30日19:31:58
 */
@Slf4j
public class CommonUtils {

    private static SysConfigRedis sysConfigRedis;
    private static SysConfigService sysConfigService;

    static {
        CommonUtils.sysConfigRedis = (SysConfigRedis) SpringContextUtils.getBean("sysConfigRedis");
        CommonUtils.sysConfigService = (SysConfigService) SpringContextUtils.getBean("sysConfigService");
    }

    /**
     * 判断是否在系统营业时间
     * @return
     */
    public static boolean executeLegalTime() {
        sysConfigRedis.delete(CcConstants.SYS_BUSINESS_START_TIME);
        sysConfigRedis.delete(CcConstants.SYS_BUSINESS_END_TIME);
        String startTime = sysConfigService.getValue(CcConstants.SYS_BUSINESS_START_TIME, null);
        String endTime = sysConfigService.getValue(CcConstants.SYS_BUSINESS_END_TIME, null);
        if(StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
            log.error("系统营业开始、结束时间未配置");
            throw new CCException("系统营业开始、结束时间未配置");
        }
        Date date = new Date();
        String day = DateUtils.date2String(date, "yyyyMMdd");

        String startTimeStr = day+startTime.replaceAll(":", "")+"00";
        String endTimeStr = day+endTime.replaceAll(":", "")+"00";
        log.debug("系统营业开始时间"+startTimeStr);
        log.debug("系统营业结束时间"+endTimeStr);
        long startTimeDetail = DateUtils.string2Date(startTimeStr, "yyyyMMddHHmmss").getTime();
        long endTimeDetail = DateUtils.string2Date(endTimeStr,"yyyyMMddHHmmss").getTime();
        long nowTime = date.getTime();
        if(nowTime >= startTimeDetail && nowTime <= endTimeDetail){
            return true;
        }else{
            return false;
        }
    }
}
